package com.tlp.tlp2k11;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class AccelerometerTlp extends Activity implements OnTouchListener, SensorEventListener {
	 
	private static final int MOVE_SENSOR_FACTOR = 50;
	 
	 private SensorManager sensorManager;
	 private Sensor accelerometer;
	 
	 private float xActionDown = 0;
	 private float yActionDown = 0;
	 private float xActionMove = 0;
	 private float yActionMove = 0;
	 
	 private float xInitialSensor = 0;
	 private float yInitialSensor = 0;
	 private float xSensor = 0;
	 private float ySensor = 0;
	 private boolean firstOnSensor = true;
	 
	 private TextView textTenerife;
	 private TextView textLan;
	 private TextView textParty;
	 private TextView text2k11;
	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accelerometer);
		
		textTenerife = (TextView)findViewById(R.id.textTenerife);
		textLan = (TextView)findViewById(R.id.textLan);
		textParty = (TextView)findViewById(R.id.textParty);
		text2k11 = (TextView)findViewById(R.id.text2k11);
		
		textTenerife.setOnTouchListener(this);
		textLan.setOnTouchListener(this);
		textParty.setOnTouchListener(this);
		
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		sensorManager.unregisterListener(this);
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			xActionDown = event.getX();
			yActionDown = event.getY();
			xActionMove = xActionDown;
			yActionMove = yActionDown;
		} if (event.getAction() == MotionEvent.ACTION_MOVE) {
			Animation animation = new TranslateAnimation(xActionMove, event.getX(), yActionMove, event.getY());
			animation.setFillAfter(true);

			xActionMove = event.getX();
			yActionMove = event.getY();
			switch (v.getId()) {
			case R.id.textTenerife:
				textTenerife.startAnimation(animation);
				break;
			case R.id.textLan:
				textLan.startAnimation(animation);
				break;
			case R.id.textParty:
				textParty.startAnimation(animation);
				break;
			}
		} if (event.getAction() == MotionEvent.ACTION_UP) {
			Animation animation = new TranslateAnimation(xActionMove, xActionDown, yActionMove, yActionDown);
			animation.setInterpolator(new DecelerateInterpolator());
			animation.setDuration(300);
			switch (v.getId()) {
			case R.id.textTenerife:
				textTenerife.startAnimation(animation);
				break;
			case R.id.textLan:
				textLan.startAnimation(animation);
				break;
			case R.id.textParty:
				textParty.startAnimation(animation);
				break;
			}
		}

		return true;
	}

	
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
		
		if (firstOnSensor) {
			firstOnSensor = false;
			xInitialSensor = event.values[0];
			yInitialSensor = event.values[1];
		}
		
		float currentXSensor = -(event.values[0] - xInitialSensor) * MOVE_SENSOR_FACTOR;
		float currentYSensor = (event.values[1] - yInitialSensor) * MOVE_SENSOR_FACTOR;
		
		Animation animation = new TranslateAnimation(xSensor, currentXSensor, ySensor, currentYSensor);
		animation.setFillAfter(true);
		
		xSensor = currentXSensor;
		ySensor = currentYSensor;
		
		text2k11.startAnimation(animation);
	}

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	
}
